package com.dlc.garbagerecyclebox.bean;

import android.text.TextUtils;
import com.dlc.commonbiz.base.serialport.utils.ByteUtil;
import com.dlc.commonbiz.base.util.GsonUtil;
import com.dlc.garbagerecyclebox.FaultCode;
import com.dlc.garbagerecyclebox.GarbageRecyclerBoxProtocol;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :      fangbingran
 * @aescription : 设备信息
 * @date :        2019/07/30  20:13
 */
public class DeviceInfoBean implements Cloneable {
    /**
     * 命令码
     */
    private byte[] cmdCode;
    /**
     * 设备地址
     */
    private int deviceAddress;
    /**
     * 命令码流水号
     */
    private int cmdCodeNumber;
    /**
     * /**
     * 主流程
     */
    private byte mainProcessCmd;
    /**
     * 执行类别
     */
    private byte executeCategory;
    /**
     * 故障信息
     */
    private FaultCodeBean faultCodeBean;
    /**
     * 传感器信息
     */
    private SensorStatusBean sensorStatusBean;

    public DeviceInfoBean() {
    }

    public DeviceInfoBean(DeviceInfoBean deviceInfoBean) {
        cmdCode = deviceInfoBean.cmdCode;
        deviceAddress = deviceInfoBean.deviceAddress;
        cmdCodeNumber = deviceInfoBean.cmdCodeNumber;
        mainProcessCmd = deviceInfoBean.mainProcessCmd;
        executeCategory = deviceInfoBean.executeCategory;
        faultCodeBean = deviceInfoBean.faultCodeBean;
        sensorStatusBean = deviceInfoBean.sensorStatusBean;
    }

    /**
     * 是否是投递流程
     *
     * @return
     */
    public boolean isDeliveryProcess() {
        return mainProcessCmd == GarbageRecyclerBoxProtocol.CMD_DELIVERY;
    }

    /**
     * 是否是回收流程
     *
     * @return
     */
    public boolean isRecyclingProcess() {
        return mainProcessCmd == GarbageRecyclerBoxProtocol.CMD_RECYCLING;
    }

    /**
     * 是否是投递流程
     *
     * @return
     */
    public boolean isManageMaintainProcess() {
        return mainProcessCmd == GarbageRecyclerBoxProtocol.CMD_MANAGE_MAINTAIN;
    }

    public SensorStatusBean getSensorStatusBean() {
        return sensorStatusBean;
    }

    public void setSensorStatusBean(SensorStatusBean sensorStatusBean) {
        this.sensorStatusBean = sensorStatusBean;
    }

    public FaultCodeBean getFaultCodeBean() {
        return faultCodeBean;
    }

    public void setFaultCodeBean(FaultCodeBean faultCodeBean) {
        this.faultCodeBean = faultCodeBean;
    }

    public byte getMainProcessCmd() {
        return mainProcessCmd;
    }

    public void setMainProcessCmd(byte mainProcessCmd) {
        this.mainProcessCmd = mainProcessCmd;
    }

    public byte getExecuteCategory() {
        return executeCategory;
    }

    public void setExecuteCategory(byte executeCategory) {
        this.executeCategory = executeCategory;
    }

    public int getCmdCodeNumber() {
        return cmdCodeNumber;
    }

    public void setCmdCodeNumber(int cmdCodeNumber) {
        this.cmdCodeNumber = cmdCodeNumber;
    }

    public int getDeviceAddress() {
        return deviceAddress;
    }

    public void setDeviceAddress(int deviceAddress) {
        this.deviceAddress = deviceAddress;
    }

    public byte[] getCmdCode() {
        return cmdCode;
    }

    public void setCmdCode(byte[] cmdCode) {
        this.cmdCode = cmdCode;
    }

    @Override
    public String toString() {
        return "命令码："
                + ByteUtil.bytes2HexStr(cmdCode)
                + ",设备地址:"
                + deviceAddress
                + ",命令码流水号:"
                + cmdCodeNumber
                + ",主流程:"
                + mainProcessCmd
                + ",执行类别:"
                + executeCategory
                + ","
                + (faultCodeBean == null ? "" : faultCodeBean.toString())
                + ","
                + (sensorStatusBean == null ? "" : sensorStatusBean.toString());
    }

    /**
     * @author :      fangbingran
     * @aescription : 错误码
     * @date :        2019/07/26  21:48
     */
    public static class FaultCodeBean implements Cloneable {
        private List<String> faultCode = new ArrayList<>();
        private boolean isFault;
        private String faultMsg;

        public void setFaultCode(List<String> faultCode) {
            this.faultCode = faultCode;
        }

        public List<String> getFaultCode() {
            return faultCode;
        }

        public FaultCodeBean(String hexString) {
            getFault(hexString);
        }

        public FaultCodeBean() {
        }

        private String hexString;
        private String faultBit;

        private void getFault(String hexString) {
            this.hexString = hexString;
            this.faultBit = ByteUtil.hexStr2BinArr(hexString);
            int faultBitLength = faultBit.length();
            //数据16个bit,两个字节
            if (faultBitLength == 16) {
                //0000000000010010
                if (faultBit.substring(faultBitLength - 2, faultBitLength).equals("01")) {
                    faultCode.add(FaultCode.OPEN_DOOR_OVERTIME);
                } else if (faultBit.substring(faultBitLength - 2, faultBitLength).equals("10")) {
                    faultCode.add(FaultCode.OPEN_DOOR_TOO_SHORT);
                } else if (faultBit.substring(faultBitLength - 2, faultBitLength).equals("11")) {
                    faultCode.add(FaultCode.OPEN_DOOR_MOTOR);
                }

                if (faultBit.substring(faultBitLength - 4, faultBitLength - 2).equals("01")) {
                    faultCode.add(FaultCode.CLOSE_DOOR_OVERTIME);
                } else if (faultBit.substring(faultBitLength - 4, faultBitLength - 2).equals("10")) {
                    faultCode.add(FaultCode.CLOSE_DOOR_TOO_SHORT);
                } else if (faultBit.substring(faultBitLength - 4, faultBitLength - 2).equals("11")) {
                    faultCode.add(FaultCode.CLOSE_DOOR_MOTOR);
                }
                if (faultBit.substring(faultBitLength - 5, faultBitLength - 4).equals("1")) {
                    faultCode.add(FaultCode.HATCH_DOOR_ANTI_CLIP_HAND);
                }
                if (faultBit.substring(faultBitLength - 6, faultBitLength - 5).equals("1")) {
                    faultCode.add(FaultCode.FAILED_TO_OPEN_CAMERA);
                }
                if (faultBit.substring(faultBitLength - 7, faultBitLength - 6).equals("1")) {
                    faultCode.add(FaultCode.CONVEYOR_BELT_MOTOR_OVER_CURRENT);
                }
                if (faultBit.substring(faultBitLength - 8, faultBitLength - 7).equals("1")) {
                    faultCode.add(FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR_OVER_TIME);
                }
                if (faultBit.substring(faultBitLength - 9, faultBitLength - 8).equals("1")) {
                    faultCode.add(FaultCode.LOWER_STOREHOUSE_DOOR_OPEN_DOOR);
                }
                if (faultBit.substring(faultBitLength - 10, faultBitLength - 9).equals("1")) {
                    faultCode.add(FaultCode.VISUAL_COMMUNICATION);
                }
                if (faultBit.substring(faultBitLength - 11, faultBitLength - 10).equals("1")) {
                    faultCode.add(FaultCode.ELECTRONIC_SCALE_COMMUNICATION);
                }
                if (faultBit.substring(faultBitLength - 12, faultBitLength - 11).equals("1")) {
                    faultCode.add(FaultCode.ELECTRONIC_SCALE_HARDWARE);
                }
                if (faultBit.substring(faultBitLength - 13, faultBitLength - 12).equals("1")) {
                    faultCode.add(FaultCode.VERTICAL_RANGING_COMMUNICATION);
                }
                if (faultBit.substring(faultBitLength - 14, faultBitLength - 13).equals("1")) {
                    faultCode.add(FaultCode.VERTICAL_RANGING_HARDWARE);
                }
                if (faultBit.substring(faultBitLength - 15, faultBitLength - 14).equals("1")) {
                    faultCode.add(FaultCode.COMMUNICATION);
                }
                if (faultCode.size() <= 0) {
                    setFault(false);
                    setFaultMsg("设备正常");
                } else {
                    setFault(true);
                    setFaultMsg(GsonUtil.getInstance()
                            .parseObjToJsonStr(faultCode));
                }
            } else {
                setFault(true);
                setFaultMsg("数据异常");
            }

        }

        public boolean isHasFault(String fault) {
            for (int i = 0; i < faultCode.size(); i++) {
                String value = faultCode.get(i);
                if (TextUtils.equals(fault, value)) {
                    return true;
                }

            }
            return false;
        }

        @Override
        public String toString() {
            return "故障码原数据:" + hexString + " ,转换为bit数据:" + faultBit + ",结果解析:{" + (faultCode.size() > 0 ? GsonUtil.getInstance()
                    .parseObjToJsonStr(faultCode) : "设备正常") + '}';
        }

        public boolean isFault() {
            return isFault;
        }

        public void setFault(boolean fault) {
            isFault = fault;
        }

        public String getFaultMsg() {
            return faultMsg;
        }

        public void setFaultMsg(String faultMsg) {
            this.faultMsg = faultMsg;
        }

        @Override
        public FaultCodeBean clone() {
            FaultCodeBean faultCodeBean = null;
            try {
                faultCodeBean = (FaultCodeBean) super.clone();
                if (faultCode != null) {
                    faultCodeBean.setFaultCode(new ArrayList<>(faultCode));
                }
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return faultCodeBean;
        }
    }

    /**
     * 是否是主柜
     *
     * @return
     */
    public boolean isMainCabinet() {
        return deviceAddress == 1;
    }

    /**
     * @author :      fangbingran
     * @aescription : 传感器状态
     * @date :        2019/07/26  22:30
     */
    public static class SensorStatusBean implements Cloneable {

        /**
         * 上舱门上限位
         */
        private boolean isUpperHatchLimit = false;

        /**
         * 上舱门下限位
         */
        private boolean isUpperHatchLowerLimit = false;
        /**
         * 左夹手
         */
        private boolean isLeftClasp = false;
        /**
         * 右夹手
         */
        private boolean isRightClasp = false;
        /**
         * 电磁锁上限位
         */
        private boolean isELockUpperLimit = false;
        /**
         * 电磁锁下限位
         */
        private boolean isELockUpperLowerLimit = false;
        /**
         * 传送带第一光电
         */
        private boolean isConveyorBeltOnePhotoelectric = false;
        /**
         * 传送带第二光电
         */
        private boolean isConveyorBeltTwoPhotoelectric = false;
        /**
         * 传送带第三光电
         */
        private boolean isConveyorBeltThreePhotoelectric = false;
        /**
         * 箱满横向光电1
         */
        private boolean isBoxFullTransversePhotoelectricOne = false;
        /**
         * 箱满横向光电2
         */
        private boolean isBoxFullTransversePhotoelectricTwo = false;
        /**
         * 箱满横向光电3
         */
        private boolean isBoxFullTransversePhotoelectricThree = false;
        /**
         * 烟雾感应
         */
        private boolean isSmokeSensing = false;
        /**
         * 人体感应
         */
        private boolean isPeopleSensing = false;
        /**
         * 是否打開传送带曝光灯
         */
        private boolean isOpenConveyorBeltExposureLamp = false;
        /**
         * 箱内照明灯
         */
        private boolean isLightingLampInBox = false;
        /**
         * 箱内风扇
         */
        private boolean isFanInBox = false;
        /**
         * 箱顶指示灯-蓝色
         */
        private boolean isBoxTopIndicatorBlue = false;
        /**
         * 箱顶指示灯-绿色
         */
        private boolean isBoxTopIndicatorGreen = false;
        /**
         * 箱顶指示灯-红色
         */
        private boolean isBoxTopIndicatorRed = false;

        /**
         * 外部照命灯
         */
        private boolean isOpenExteriorLights = false;
        /**
         * 广告灯
         */
        private boolean isOpenAdvertisingLights = false;
        /**
         * 温度值
         */
        private float temperatureValue;
        /**
         * （副柜）累计重量值
         * （主柜）累计饮料瓶数量
         */
        private float totalAmount;
        /**
         * （副柜）累计重量值
         * （主柜）累计饮料瓶数量
         */
        private float realTimeAmount;
        /**
         * 垂直测距高度
         */
        private float verticalRangingHeight;
        /**
         * 是否打开有害开门锁
         */
        private boolean isOpenHarmfulDoorLock;
        /**
         * 箱满百分比
         */
        private int boxFullPercentage;
        private boolean isDataFault;
        private String faultMsg;
        /**
         * 设备地址
         */
        private int deviceAddress;
        private String hexString;
        private String sensorIOBit;

        public boolean isUpperHatchLimit() {
            return isUpperHatchLimit;
        }

        public void setUpperHatchLimit(boolean isUpperHatchLimit) {
            this.isUpperHatchLimit = isUpperHatchLimit;
        }

        public boolean isUpperHatchLowerLimit() {
            return isUpperHatchLowerLimit;
        }

        public void setUpperHatchLowerLimit(boolean upperHatchLowerLimit) {
            isUpperHatchLowerLimit = upperHatchLowerLimit;
        }

        public boolean isLeftClasp() {
            return isLeftClasp;
        }

        public void setLeftClasp(boolean leftClasp) {
            isLeftClasp = leftClasp;
        }

        public boolean isRightClasp() {
            return isRightClasp;
        }

        public void setRightClasp(boolean rightClasp) {
            isRightClasp = rightClasp;
        }

        public boolean isELockUpperLimit() {
            return isELockUpperLimit;
        }

        public void setELockUpperLimit(boolean ELockUpperLimit) {
            isELockUpperLimit = ELockUpperLimit;
        }

        public boolean isELockUpperLowerLimit() {
            return isELockUpperLowerLimit;
        }

        public void setELockUpperLowerLimit(boolean ELockUpperLowerLimit) {
            isELockUpperLowerLimit = ELockUpperLowerLimit;
        }

        public boolean isConveyorBeltOnePhotoelectric() {
            return isConveyorBeltOnePhotoelectric;
        }

        public void setConveyorBeltOnePhotoelectric(boolean conveyorBeltOnePhotoelectric) {
            isConveyorBeltOnePhotoelectric = conveyorBeltOnePhotoelectric;
        }

        public boolean isConveyorBeltTwoPhotoelectric() {
            return isConveyorBeltTwoPhotoelectric;
        }

        public void setConveyorBeltTwoPhotoelectric(boolean conveyorBeltTwoPhotoelectric) {
            isConveyorBeltTwoPhotoelectric = conveyorBeltTwoPhotoelectric;
        }

        public boolean isConveyorBeltThreePhotoelectric() {
            return isConveyorBeltThreePhotoelectric;
        }

        public void setConveyorBeltThreePhotoelectric(boolean conveyorBeltThreePhotoelectric) {
            isConveyorBeltThreePhotoelectric = conveyorBeltThreePhotoelectric;
        }

        public boolean isBoxFullTransversePhotoelectricOne() {
            return isBoxFullTransversePhotoelectricOne;
        }

        public void setBoxFullTransversePhotoelectricOne(boolean boxFullTransversePhotoelectricOne) {
            isBoxFullTransversePhotoelectricOne = boxFullTransversePhotoelectricOne;
        }

        public boolean isBoxFullTransversePhotoelectricTwo() {
            return isBoxFullTransversePhotoelectricTwo;
        }

        public void setBoxFullTransversePhotoelectricTwo(boolean boxFullTransversePhotoelectricTwo) {
            isBoxFullTransversePhotoelectricTwo = boxFullTransversePhotoelectricTwo;
        }

        public boolean isBoxFullTransversePhotoelectricThree() {
            return isBoxFullTransversePhotoelectricThree;
        }

        public void setBoxFullTransversePhotoelectricThree(boolean boxFullTransversePhotoelectricThree) {
            isBoxFullTransversePhotoelectricThree = boxFullTransversePhotoelectricThree;
        }

        public boolean isSmokeSensing() {
            return isSmokeSensing;
        }

        public void setSmokeSensing(boolean smokeSensing) {
            isSmokeSensing = smokeSensing;
        }

        public boolean isPeopleSensing() {
            return isPeopleSensing;
        }

        public void setPeopleSensing(boolean peopleSensing) {
            isPeopleSensing = peopleSensing;
        }

        public boolean isOpenConveyorBeltExposureLamp() {
            return isOpenConveyorBeltExposureLamp;
        }

        public void setOpenConveyorBeltExposureLamp(boolean openConveyorBeltExposureLamp) {
            isOpenConveyorBeltExposureLamp = openConveyorBeltExposureLamp;
        }

        public boolean isLightingLampInBox() {
            return isLightingLampInBox;
        }

        public void setLightingLampInBox(boolean lightingLampInBox) {
            
            isLightingLampInBox = lightingLampInBox;
        }

        public boolean isFanInBox() {
            return isFanInBox;
        }

        public void setFanInBox(boolean fanInBox) {
            isFanInBox = fanInBox;
        }

        public boolean isBoxTopIndicatorWhite() {
            return isBoxTopIndicatorBlue && isBoxTopIndicatorRed && isBoxTopIndicatorGreen;
        }

        public void setBoxTopIndicatorBlue(boolean isBoxTopIndicatorBlue) {
            this.isBoxTopIndicatorBlue = isBoxTopIndicatorBlue;
        }

        public boolean isBoxTopIndicatorGreen() {
            return isBoxTopIndicatorGreen;
        }

        public boolean isBoxTopIndicatorBlue() {
            return isBoxTopIndicatorBlue;
        }

        public void setBoxTopIndicatorGreen(boolean boxTopIndicatorGreen) {
            isBoxTopIndicatorGreen = boxTopIndicatorGreen;
        }

        public boolean isBoxTopIndicatorRed() {
            return isBoxTopIndicatorRed;
        }

        public void setBoxTopIndicatorRed(boolean boxTopIndicatorRed) {
            isBoxTopIndicatorRed = boxTopIndicatorRed;
        }

        public float getTemperatureValue() {
            return temperatureValue;
        }

        public void setTemperatureValue(float temperatureValue) {
            this.temperatureValue = temperatureValue;
        }

        public float getTotalAmount() {
            return totalAmount;
        }

        public void setTotalAmount(float totalAmount) {
            this.totalAmount = totalAmount;
        }

        public float getRealTimeAmount() {
            return realTimeAmount;
        }

        public void setRealTimeAmount(float realTimeAmount) {
            this.realTimeAmount = realTimeAmount;
        }

        public float getVerticalRangingHeight() {
            return verticalRangingHeight;
        }

        public void setVerticalRangingHeight(float verticalRangingHeight) {
            this.verticalRangingHeight = verticalRangingHeight;
        }

        public float getBoxFullPercentage() {
            return boxFullPercentage;
        }

        public void setBoxFullPercentage(int boxFullPercentage) {
            this.boxFullPercentage = boxFullPercentage;
        }

        public SensorStatusBean(byte[] hexString) {
            handleData(hexString);
        }

        private void handleData(byte[] hex) {//解析心跳
            this.hexString = ByteUtil.bytes2HexStr(hex);
            //数据24个字节
            if (hex != null && hex.length == 24) {
                sensorIOBit = ByteUtil.hexStr2BitArr(ByteUtil.bytes2HexStr(hex, 0, 4));
                //Log.i("lgq","心跳数据===== "+sensorIOBit);
                int sensorIOBitLength = sensorIOBit.length();
                if (sensorIOBit.substring(sensorIOBitLength - 1, sensorIOBitLength).equals("1")) {
                    //Log.e("lgq","上报心跳ttttt");
                    setUpperHatchLimit(true);
                }else {
                    //Log.i("lgq","上报心跳ffffff");
                }

                if (sensorIOBit.substring(sensorIOBitLength - 2, sensorIOBitLength - 1).equals("1")) {
                    setUpperHatchLowerLimit(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 3, sensorIOBitLength - 2).equals("1")) {
                    setLeftClasp(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 4, sensorIOBitLength - 3).equals("1")) {
                    setRightClasp(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 5, sensorIOBitLength - 4).equals("1")) {
                    setELockUpperLimit(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 6, sensorIOBitLength - 5).equals("1")) {
                    //Log.e("lgq","下锁上报心跳ttttt "+sensorIOBit.substring(sensorIOBitLength - 6, sensorIOBitLength - 5));
                    setELockUpperLowerLimit(true);
                }else {
                    //Log.e("lgq","下锁上报心跳ttttt "+sensorIOBit.substring(sensorIOBitLength - 6, sensorIOBitLength - 5));
                }
                if (sensorIOBit.substring(sensorIOBitLength - 7, sensorIOBitLength - 6).equals("1")) {
                    setConveyorBeltOnePhotoelectric(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 8, sensorIOBitLength - 7).equals("1")) {
                    setConveyorBeltTwoPhotoelectric(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 9, sensorIOBitLength - 8).equals("1")) {
                    setConveyorBeltThreePhotoelectric(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 10, sensorIOBitLength - 9).equals("1")) {
                    setBoxFullTransversePhotoelectricOne(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 11, sensorIOBitLength - 10).equals("1")) {
                    setBoxFullTransversePhotoelectricTwo(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 12, sensorIOBitLength - 11).equals("1")) {
                    setBoxFullTransversePhotoelectricThree(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 13, sensorIOBitLength - 12).equals("1")) {
                    setSmokeSensing(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 14, sensorIOBitLength - 13).equals("1")) {
                    setPeopleSensing(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 15, sensorIOBitLength - 14).equals("1")) {
                    setOpenHarmfulDoorLock(true);
                }
                //保留IOBit第14,15个bit
                if (sensorIOBit.substring(sensorIOBitLength - 17, sensorIOBitLength - 16).equals("1")) {
                    setOpenConveyorBeltExposureLamp(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 18, sensorIOBitLength - 17).equals("1")) {
                    setLightingLampInBox(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 19, sensorIOBitLength - 18).equals("1")) {
                    setFanInBox(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 20, sensorIOBitLength - 19).equals("1")) {
                    setBoxTopIndicatorBlue(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 21, sensorIOBitLength - 20).equals("1")) {
                    setBoxTopIndicatorRed(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 22, sensorIOBitLength - 21).equals("1")) {
                    setBoxTopIndicatorGreen(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 23, sensorIOBitLength - 22).equals("1")) {
                    setOpenExteriorLights(true);
                }
                if (sensorIOBit.substring(sensorIOBitLength - 24, sensorIOBitLength - 23).equals("1")) {
                    setOpenAdvertisingLights(true);
                }
                //保留IOBit第24,31个bit

                setTemperatureValue(ByteUtil.hexStr2Float(ByteUtil.getBytes(hex, 4, 4)));
                //Log.d("wd=lgq==心跳解析====",+ByteUtil.hexStr2Float(ByteUtil.getBytes(hex, 4, 4))+"...几号箱-----");

                setTotalAmount(ByteUtil.hexStr2Float(ByteUtil.getBytes(hex, 8, 4)));
                setRealTimeAmount(ByteUtil.hexStr2Float(ByteUtil.getBytes(hex, 12, 4)));
                setVerticalRangingHeight(ByteUtil.hexStr2Float(ByteUtil.getBytes(hex, 16, 4)));
                setBoxFullPercentage(ByteUtil.byteToInt(ByteUtil.getBytes(hex, 20, 1)));

                //保留hexString第21,23个字节

            } else {
                setDataFault(true);
                setFaultMsg("数据异常");
            }
        }

        public boolean isDataFault() {
            return isDataFault;
        }

        public void setDataFault(boolean dataFault) {
            isDataFault = dataFault;
        }

        public String getFaultMsg() {
            return faultMsg;
        }

        public void setFaultMsg(String faultMsg) {
            this.faultMsg = faultMsg;
        }

        /**
         * 是否是主柜
         *
         * @return
         */
        public boolean isMainCabinet() {
            return deviceAddress == 1;
        }

        @Override
        public String toString() {
            return "传感器原数据:"
                    + hexString
                    + " ,转换为bit数据:"
                    + sensorIOBit
                    + ",结果:"
                    + geExplain()
                    + ",溫度值:"
                    + temperatureValue
                    + strRrealTimeAmount()
                    + strTotalAmount()
                    + ",垂直测距高度:"
                    + getVerticalRangingHeight() + ",有害门是否开启:"
                    + isOpenHarmfulDoorLock()
                    + ",箱滿百分比："
                    + boxFullPercentage;
        }

        private String strTotalAmount() {
            if (isMainCabinet()) {
                return "累计饮料瓶数量:" + totalAmount;
            }
            return "累计重量值:" + totalAmount;
        }

        private String strRrealTimeAmount() {
            if (isMainCabinet()) {
                return ",当前实际饮料瓶数量:" + realTimeAmount;
            }
            return ",当前实际重量值:" + realTimeAmount;
        }

        private String geExplain() {
            StringBuilder stringBuilder = new StringBuilder();

            if (isUpperHatchLimit()) {
                stringBuilder.append("上舱门上限位,");
            }
            if (isUpperHatchLowerLimit()) {
                stringBuilder.append("上舱门下限位,");
            }
            if (isLeftClasp()) {
                stringBuilder.append("左夹手,");
            }
            if (isRightClasp()) {
                stringBuilder.append("右夹手,");
            }
            if (isELockUpperLimit()) {
                stringBuilder.append("电磁锁上限位,");
            }
            if (isELockUpperLowerLimit()) {
                stringBuilder.append("电磁锁下限位,");
            }
            if (isConveyorBeltOnePhotoelectric()) {
                stringBuilder.append("传送带第一光电,");
            }
            if (isConveyorBeltTwoPhotoelectric()) {
                stringBuilder.append("传送带第第二光电,");
            }
            if (isConveyorBeltThreePhotoelectric()) {
                stringBuilder.append("传送带第第三光电,");
            }
            if (isBoxFullTransversePhotoelectricOne()) {
                stringBuilder.append("箱满横向光电1,");
            }
            if (isBoxFullTransversePhotoelectricTwo()) {
                stringBuilder.append("箱满横向光电2,");
            }
            if (isBoxFullTransversePhotoelectricThree()) {
                stringBuilder.append("箱满横向光电3，");
            }
            if (isSmokeSensing()) {
                stringBuilder.append("烟雾感应");
            }
            if (isPeopleSensing()) {
                stringBuilder.append("人体感应");
            }
            if (isOpenConveyorBeltExposureLamp()) {
                stringBuilder.append("是否打開传送带曝光灯，");
            }
            if (isLightingLampInBox()) {
                stringBuilder.append("箱内照明灯，");
            }
            if (isFanInBox()) {
                stringBuilder.append("箱内风扇，");
            }
            if (isBoxTopIndicatorWhite()) {
                stringBuilder.append("箱顶指示灯-白色，");
            } else {
                if (isBoxTopIndicatorBlue()) {
                    stringBuilder.append("箱顶指示灯-蓝色，");
                }
                if (isBoxTopIndicatorGreen()) {
                    stringBuilder.append("箱顶指示灯-绿色，");
                }
                if (isBoxTopIndicatorRed()) {
                    stringBuilder.append("箱顶指示灯-红色，");
                }
                if (isOpenExteriorLights()) {
                    stringBuilder.append("外部照命灯，");
                }
            }

            return stringBuilder.toString().length() == 0 ? "未知状态:" : stringBuilder.toString();
        }

        public int getDeviceAddress() {
            return deviceAddress;
        }

        public void setDeviceAddress(int deviceAddress) {
            this.deviceAddress = deviceAddress;
        }

        public boolean isOpenExteriorLights() {
            return isOpenExteriorLights;
        }

        public void setOpenExteriorLights(boolean openExteriorLights) {
            isOpenExteriorLights = openExteriorLights;
        }

        public boolean isOpenAdvertisingLights() {
            return isOpenAdvertisingLights;
        }

        public void setOpenAdvertisingLights(boolean openAdvertisingLights) {
            isOpenAdvertisingLights = openAdvertisingLights;
        }

        public boolean isOpenHarmfulDoorLock() {
            return isOpenHarmfulDoorLock;
        }

        public void setOpenHarmfulDoorLock(boolean openHarmfulDoorLock) {
            isOpenHarmfulDoorLock = openHarmfulDoorLock;
        }

        @Override
        public SensorStatusBean clone() {
            SensorStatusBean sensorStatusBean = null;
            try {
                sensorStatusBean = (SensorStatusBean) super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return sensorStatusBean;
        }
    }

    @Override
    public DeviceInfoBean clone() {
        DeviceInfoBean deviceInfoBean = null;
        try {
            deviceInfoBean = (DeviceInfoBean) super.clone();
            if (cmdCode != null) {
                deviceInfoBean.setCmdCode(ByteUtil.getBytes(cmdCode, 0, cmdCode.length));
            }
            if (faultCodeBean != null) {
                deviceInfoBean.setFaultCodeBean(faultCodeBean.clone());
            }
            if (sensorStatusBean != null) {
                deviceInfoBean.setSensorStatusBean(sensorStatusBean.clone());
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return deviceInfoBean;
    }
}
